﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Xml;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Groupbox;
using System.Windows.Browser;
using Microsoft.SilverlightMediaFramework.Core;
using Microsoft.SilverlightMediaFramework.Core.Media;






namespace OntoWPFSilverlight
{
    public partial class MainPage : UserControl
    {
        private Object form = new Widget.CanvasForm();

        public MainPage()
        {
                         
            InitializeComponent();
            //System.Windows.Browser.HtmlPage.Window.Alert("main");
            LoadXMLFile();
        }


        private void LoadXMLFile()
        {
            WebClient xmlClient = new WebClient();
            xmlClient.OpenReadCompleted += new OpenReadCompletedEventHandler(xmlClient_OpenReadCompleted);
            Uri uri = new Uri("http://localhost:2984/ClientBin/saveFileDialog4.xml", UriKind.RelativeOrAbsolute);
            xmlClient.OpenReadAsync(uri);


        }


        

        void xmlClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                HtmlPage.Window.Alert("xmlClient_OpenReadCompleted error" + e.Error.ToString());
                return;
            }


            using (Stream s = e.Result)
            {
                
                try
                {
                    
                    Class1 classe = new Class1();
                    classe.readXml(s, ref form);
                    ((Widget.CanvasForm)form).update();
                    
                }
                catch (Exception ex)
                {
                    HtmlPage.Window.Alert("xmlClient_OpenReadCompleted ex: " + ex);
                }

                LayoutRoot.Children.Add((Widget.CanvasForm)form);

                #region Commento
                //Slider slider = new Slider();
                //byte r, g, b;

                //foreach (XElement el in elements)
                //{
                //  string type = el.Attribute("Type").Value.Split('.')[el.Attribute("Type").Value.Split('.').Length - 1];

                //OutputTextBlock.Text += type;
                //switch (type)
                //{
                //#region Form
                //case "Form":

                //    border.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    border.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    myCanvas.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));
                //    //string a = el.Element("Appearance").Element("BackColor").Attribute("Value").Value;
                //    break;
                //#endregion

                //#region ListBox
                //case "ListBox":

                //    Widget.Listbox listbox1 = new Widget.Listbox();
                //    listbox1.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    listbox1.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);
                //    Canvas.SetLeft(listbox1, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                //    Canvas.SetTop(listbox1, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));


                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    listbox1.Background=new SolidColorBrush(Color.FromArgb(255,r,g,b));

                //    if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                //    {
                //        IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                //        listbox1.AddItem(nodes);   
                //    }

                //    listbox1.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                //    myCanvas.Children.Add(listbox1);
                //    break;
                //#endregion

                //#region ComboBox
                //case "ComboBox":

                //    Widget.ComboBox comboBox = new Widget.ComboBox();

                //    comboBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    comboBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);
                //    Canvas.SetLeft(comboBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                //    Canvas.SetTop(comboBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));

                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    comboBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                //    if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                //    {
                //        IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                //        comboBox.AddItem(nodes);   
                //    }

                //    comboBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                //    myCanvas.Children.Add(comboBox);

                //    break;
                //#endregion

                //#region GroupCheckBox
                //case "GroupCheckBox":

                //    Widget.GroupCheckBox groupCheckBox = new Widget.GroupCheckBox();

                //    groupCheckBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    groupCheckBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                //    Canvas.SetLeft(groupCheckBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                //    Canvas.SetTop(groupCheckBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));


                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    groupCheckBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                //    if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                //    {
                //        IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                //        groupCheckBox.AddItem(nodes);
                //    }

                //    groupCheckBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                //    myCanvas.Children.Add(groupCheckBox);

                //    break;
                //#endregion

                //#region RadioGroupBox

                //case "RadioGroupBox":
                //    Widget.RadioGroupBox radioGroupBox = new Widget.RadioGroupBox();

                //    radioGroupBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    radioGroupBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                //    Canvas.SetLeft(radioGroupBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                //    Canvas.SetTop(radioGroupBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));

                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    radioGroupBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                //    if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                //    {
                //        IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                //        radioGroupBox.AddItem(nodes);
                //    }

                //    radioGroupBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                //    myCanvas.Children.Add(radioGroupBox);

                //    break;

                //#endregion

                //#region Slider

                //case "Slider" :

                //    Widget.Slider mySlider = new Widget.Slider();

                //    mySlider.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                //    mySlider.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                //    Canvas.SetLeft(mySlider, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                //    Canvas.SetTop(mySlider, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));


                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                //    byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                //    mySlider.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                //    if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                //    {
                //        IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                //        mySlider.AddItem(nodes);
                //    }


                //    break;

                //#endregion






                //  }


                //                 }

                //               LayoutRoot.Children.Add(border);
                //         }
                #endregion
            }

        }

    }
}
